import cv2
import imutils
import numpy as np
from fuzzy3 import Gdt
from fuzzy3 import Ai
from fuzzy3 import compare

def Fuzzyscan(binaryimage,grayimage) :
    area=len(binaryimage[binaryimage==0])
    gm=cv2.mean(grayimage)[0]
    x=[]

    #对比度计算
    m, n = grayimage.shape
    img1_ext = cv2.copyMakeBorder(grayimage,1,1,1,1,cv2.BORDER_REPLICATE) 
    rows_ext,cols_ext = img1_ext.shape
    
                    
    b = 0.0
    for i in range(1,rows_ext-1):
        for j in range(1,cols_ext-1):
            b += ((img1_ext[i,j]-img1_ext[i,j+1])**2 + (img1_ext[i,j]-img1_ext[i,j-1])**2 + 
                    (img1_ext[i,j]-img1_ext[i+1,j])**2 + (img1_ext[i,j]-img1_ext[i-1,j])**2)

    cg = b/(4*(m-2)*(n-2)+3*(2*(m-2)+2*(n-2))+2*4) #对应上面48的计算公式


    #gdt先是填涂再是未填涂
    if compare(Ai(Gdt(gm,127.8321104,84.31149125),Gdt(area,691.8513514,91809),Gdt(cg,24212.95928,2095307.229)),Ai(Gdt(gm,201.265412,78.53247543),Gdt(area,11.1565254,249032.0686),Gdt(cg,16166.92781,598052.5273)))==1 :
            x.append("有瑕疵")
    
    if len(x)!=0:
        return (' '.join(x))
  
   
    
        
    